import sys

import numpy as np

sys.path.append("..")
from modules.tn import TemporalNetwork
from modules.tools import seconds_to_hms, timestamp2int

# range1 = ["12:02:53", "12:02:58"]
range_ = ["04:29:59", "04:30:39"]
range_ = ["22:52:57", "22:54:54"]

# range1 = ["02:07:16", "02:44:05"]
# range_ = ["00:43:49", "01:23:03"]

# range1 = ["08:14:38", "08:16:27"]
# range2 = ["18:38:43", "18:39:36"]
# range1 = ["08:42:34", "08:42:49"]
# range2 = ["12:57:40", "12:57:55"]
# range2 = ["00:00:00", "00:00:00"]

meta_path = "s3://myjfs/meta/a37b4c8c-1eb1-4a51-b41c-804fb9a19057-66ca30085872e8b7e4715ccf.json"
from storage import Boto3Storage
storage = Boto3Storage()
meta_data = storage.get_json(meta_path)
emb = storage.get_npy(meta_data.get('embedding_saved_path'))


range_ = [timestamp2int(range_[0]), timestamp2int(range_[1])]
duration = range_[1] - range_[0]
# range2 = [timestamp2int(range2[0]), timestamp2int(range2[1])]
# duration2 = range2[1] - range2[0]
# print(duration1, duration2)
# emb = np.load("/data/auto-match-dataset/yznews/287-2024-03-10-00-00-00.mfcc.npy")
# emb = np.load("/data/auto-match-dataset/yznews/287-2024-03-10-00-00-00.npy")
# emb = np.load("/data/auto-match-dataset/yznews/287-2024-03-10-00-00-00.zero.npy")
emb1 = emb[range_[0]:range_[1]+2]
# emb2 = emb[range2[0]:range2[1]]
tn = TemporalNetwork(tn_top_k=200, max_path=20, min_sim=0.5, tn_max_step=4, max_iou=1e-5, min_length=4)
matches, sims = tn(emb1, emb)
if matches.any():
    # matches = np.sort(matches, axis=0)
    for match, sim in zip(matches, sims):
        # match[[0, 2]] += range_[0]

        if match.any():
            print([seconds_to_hms(m) for m in match],
                  sim, match[2] - match[0],
                  match[3] - match[1])
